<?php

namespace App\Controller;

class RecommController extends AppBaseController
{
    private $recommLogic;
    private $posterLogic;
    private $joinLogic;

    public function _initialize()
    {
        parent::_initialize();
        $this->recommLogic = A('App/Recomm', 'Logic');
        $this->posterLogic = A("App/Poster", 'Logic');
        $this->joinLogic = A('App/Join', 'Logic');
    }

    /**
     * 收徒主页
     */
    public function home()
    {
        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];
        $incomeRes = $this->recommLogic->getIncomeSum($userId);
        $income = $incomeRes['Result'];

        $recommNumRes = $this->recommLogic->getRecommNum($userId);
        $recommNum = $recommNumRes['Result'];

        $this->assign("income", $income);
        $this->assign("recommNum", $recommNum);
        $this->display();
    }

    /**
     * 收徒列表
     */
    public function recommList()
    {
        $this->display();
    }

    /**
     * 获取收徒列表
     */
    public function doGetRecommList()
    {
        $page = I('post.page');
        $page = $page ? $page : 1;
        $size = 20;

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $listRes = $this->recommLogic->getRecommList($page, $size, $userId);

        if (!static::$res->isSuc($listRes)) {
            $this->ajaxReturn($listRes);
        }

        $data = $listRes['Result'];
        $list = $data['list'];
        $count = $data['count'];

        if (empty($list)) {
            $this->ajaxReturn(static::$res->err("暂无数据"));
        }

        $countPage = ceil($count / $size);
        $hasNext = $countPage > $page ? true : false;

        $this->assign("list", $list);
        $html = $this->fetch('doGetRecommList');

        $res = static::$res->suc("成功", array(
            "html" => $html,
            "list" => $list,
            "count" => $count,
            "hasNext" => $hasNext
        ));
        $this->ajaxReturn($res);
    }

    /**
     * 查看徒弟任务列表
     */
    public function recommTaskList()
    {
        $userId = I('get.userId');
        if (empty($userId)) {
            $this->sysErr("徒弟用户ID不能为空");
        }

        $this->assign("userId", $userId);
        $this->display();
    }

    public function doGetRecommerTaskList()
    {
        $page = I('post.page');
        $userId = I('post.userId');

        $state = 3;
        $size = 20;

        $listRes = $this->joinLogic->getJoinTaskList($page, $size, array(
            "user_id" => $userId,
            "state" => $state,
        ));

        $listInfo = $listRes['Result'];
        $list = $listInfo['list'];

        $count = $listInfo['count'];

        if (empty($list)) {
            $this->ajaxReturn(static::$res->err("暂无数据"));
        }

        $countPage = ceil($count / $size);
        $hasNext = $countPage > $page ? true : false;

        $this->assign("list", $list);
        $html = $this->fetch('doGetRecommerTaskList');
        $res = static::$res->suc("成功", array(
            "html" => $html,
            "list" => $list,
            "count" => $count,
            "hasNext" => $hasNext
        ));
        $this->ajaxReturn($res);
    }

    public function help()
    {
        $conf = $this->recommLogic->getRecommConf();
        $this->assign("conf", $conf);
        $this->display();
    }

    /**
     * 获取推广海报
     */
    public function doGetRecommPoster()
    {
        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];
        $userName = $userInfo['name'];
        $userPhoto = $userInfo['photo'];

        $res = $this->posterLogic->get($userId, $userName, $userPhoto);
        $this->ajaxReturn($res);
    }
}
